// yb_mingpian/component/customswiper/customswiper.js
var t = getApp(),
  a = t.requirejs("core"),
  c = t.requirejs("api/index");
  var loading=false;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: Array,
  },

  /**
   * 组件的初始数据
   */
  data: {
   currentIndex: 0,
   route: "card",
   menu: t.tabBar,
   menu_show: false,      
   page: 1,
   list:[],
   loaded:false,
   loading: true,
   show: true,
   display: false,
   showtabbar: false,
   tabbar_index: 0,
   id: 0,
   config: t.config
  },
  getlist: function () {
    if (loading) {
      return;
    }
    loading = true;
    var that = this,
      page = that.data.page;
      that.setData({
        loading:false
      })
    a.get('Card/CardList', {
        page:page,
        uid: getApp().getCache('userinfo') != undefined ? getApp().getCache('userinfo').uid : -1
    }, function (t) {
      that.setData({
        loading: true
      })
      loading = false;
      if (t.code == 0) {
        //console.log(111);
        that.setData({
          list: that.data.list.concat(t.info),
          page: t.info.length == 0 ? that.data.page : (that.data.page + 1),
          loaded: t.info.length < 10 ? true : false,
          show: true
        })
      } else {
        a.alert(t.msg);
      }
    }, true)
  },
  
 menu_url: function (k) {
    a.menu_url(k, 2);
  },
  loginSubmit:function(k){
      //console.log('登录获取表单id')
    var formid = k.detail.formId;
   getApp().setCache('login_formid', formid);
  
  },
  onGotUserInfo: function (q) {
    var that = this,
        e = t.getCache("userinfo");

    if (e) {
      return;
    }
    t.getUserInfo(q.detail.userInfo, function (t) {
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          that.getlist();
          that.setData({
            display: false,
            getPhoneAuth: getApp().globalData.getphone
          })
        }
      } else {
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);

  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

    async onLoad() {
      var res = await getApp().getExtC();
      var that = this;

      t.set_caidan(function(){
        a.setting();
        that.setData({
          config: getApp().config
        })
      });

    // var e = t.getCache("userinfo");
    // if (e) {
    //   that.setData({
    //     display: false
    //   })
    //   that.getlist();
    // }

      var u_info = getApp().getOpenId(function (t) {
        that.getlist();
      });
  },
  
  getlist: function () {
    if (loading) {
      return;
    }
    loading = true;
    var that = this,
      page = that.data.page;
      that.setData({
        loading:false
      })
    a.get('Card/CardList', {
        page:page,
        uid: getApp().getCache('userinfo') != undefined ? getApp().getCache('userinfo').uid : -1
    }, function (t) {
      that.setData({
        loading: true
      })
      loading = false;
      if (t.code == 0) {
        //console.log(111);
        that.setData({
          list: that.data.list.concat(t.info),
          page: t.info.length == 0 ? that.data.page : (that.data.page + 1),
          loaded: t.info.length < 10 ? true : false,
          show: true
        })
      } else {
        a.alert(t.msg);
      }
    }, true)
  },

  // 获取用户手机号
  getMobile: function (e) {
    t.globalData.getphone = false;
    this.setData({
      getPhoneAuth: false
    })
    t.getPhoneNumber(e, this, 3);
  },

  click: function (e) {
    var that = this,
      i = a.pdata(e).index;
    that.setData({
      hidden: i != that.data.hidden ? i : -1
    })
  },
  select: function (e) {
    var that = this,
      i = a.pdata(e).status;
    that.setData({
      page: 1,
      list: [],
      loaded: false,
      status: i
    })
    this.getlist()
  },
  
  
  /**
    * 下拉刷新
    */
  onPullDownRefresh: function () {
    //var e = t.getCache("userinfo");

    //if (e) {
      this.setData({
        list: [],
        page: 1,
        loaded: false
      })
      this.getlist()
      wx.stopPullDownRefresh();
    //}
   
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    //var e = t.getCache("userinfo");

    //if (e) {
    //console.log('加载更多')
    this.data.loaded || this.getlist()
    //}
  },


  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
  methods: {
    to_detail:function(e){
      var data=a.pdata(e);
      t.globalData.card_id=data.id;
      //t.setCache('card_id',data.id);
      a.jump('/yb_mingpian/pages/cardinfo/ceshi');
      // console.log(this.t.globalData.card_id)
    },
    bindconfirm: function (k) {
      //console.log(k)
      var es = t.getCache("userinfo");
      if(es){
        var e = {};
        e.formid = k.detail.formId;
        e.openid = getApp().getCache("userinfo").openid;
        e.username = getApp().getCache("userinfo").nickName;
        a.get('Market/getFormid', e, function (t) {
          //console.log(t)
        })
      }
      var id=k.detail.value.id;
      t.globalData.card_id = id;
      a.jump('/yb_mingpian/pages/cardinfo/ceshi', 4);
    },
    to_index:function(){
      a.jump('/yb_mingpian/pages/index/index', 4);
    },
    swiperChange(e) {
      this.setData({
        currentIndex: e.detail.current
      });
    }
  }
  
});